﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GenericDemo
{
    /// <summary>
    /// 泛型约束
    /// </summary>
    public class GenericConstraint
    {
        public static void ShowObject(object oParameter)
        {
            Console.WriteLine($"This is {typeof(GenericConstraint)},parameter={oParameter.GetType().Name},type={oParameter}");

            People people = (People)oParameter;
            Console.WriteLine($"People.Id = {people.Id}");
            Console.WriteLine($"People.Name = {people.Name}");
        }

        /// <summary>
        /// 基类约束
        /// 泛型的类型参数  必须是People或者People的子类  否则无法传入  这样就可以把这个类型参数当作People使用
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="oParameter"></param>
        public static void ShowGeneric<T>(T oParameter) where T : People
        {
            Console.WriteLine($"This is {typeof(GenericConstraint)},parameter={oParameter.GetType().Name},type={oParameter}");

            People people = (People)oParameter;
            Console.WriteLine($"People.Id = {people.Id}");
            Console.WriteLine($"People.Name = {people.Name}");
        }

        /// <summary>
        /// 接口约束
        /// 类型参数  必须实现这个接口 才能够传入进来
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="oParameter"></param>
        public static void ShowGeneric1<T>(T oParameter) where T : IWork
        {
            oParameter.Work();
        }
    }
}
 